package net.dfrz.lightshare;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public abstract class SessionFactoryUtil {
	protected  static SessionFactory  factory = null;

	static{
		try{
			Configuration cfg = new Configuration().configure();

			StandardServiceRegistry registrty = new StandardServiceRegistryBuilder()
			.applySettings(cfg.getProperties()).build();

			factory = cfg.buildSessionFactory(registrty);
		}
		catch(Exception e){
			
		}
	}
//	public static void before(){
//
//		Configuration cfg = new Configuration().configure();
//
//		StandardServiceRegistry registrty = new StandardServiceRegistryBuilder()
//		.applySettings(cfg.getProperties()).build();
//
//		factory = cfg.buildSessionFactory(registrty);
//
//	}
	public static Session getSession(){
		Session session = factory.getCurrentSession();
		if(session!=null){
			return session;
		}
		return factory.openSession();
	}


	public static void close(){
		factory.close();
	}

}
